import { BtMessage } from './utils/BtMessage.js'
import { BtNotification } from "./utils/BtNotification.js"

import { BtMessageBox } from './utils/BtMessageBox.js'

import LwtUtils from './api-server/utils/LwtUtils.js'

import mapper from './api-server/mapper/BaseMapper.js'
import User from './api-server/domain/pojo/User.js'

import SHA256 from './utils/sha256.js'
import { request } from './utils/request.js'

import { login, registry } from './api/index.js'

$("#message").click(() => {
    // BtMessage({
    //     type: 'success',
    //     message: '简单测试一下'
    // })  
    BtMessage({
        type: 'success',
        title: 'Title-test',
        prompt: 'time mins ago',
        message: 'this is a message',
        duration: 1500,
        style: {
            "min-width": '256px'
        }
    })
})

$("#notification").click(() => {
    BtNotification({
        type: 'success',
        title: 'Title-test',
        prompt: 'time mins ago',
        message: 'this is a message',
        duration: 1500,
        style: {
            "min-width": '256px'
        }
    })
})


$("#api-button").click(() => {
    BtMessageBox({
        type: 'success',
        title: 'success',
        message: '恭喜你啊年轻人，你成功的被我恭喜到了',
        confirmClose: true,
        onClose: (type) => {
            console.log("the close type == " + type)
        }
    })
})


$("#api-request").click(() => {
    // login("moluer", "123123").then(res => {
    //     console.log("login", res)
    //     if (res.code == 200) {
    //         localStorage.setItem("token", res.data)
    //         BtMessage({
    //             type: 'success',
    //             message: '登录成功'
    //         })
    //     }else {
    //         BtMessage({
    //             type: 'error',
    //             message: res.msg || '登录失败!请联系管理员'
    //         })
    //     }
    // })

    registry("2521459266", "123123", "2521459266@qq.com").then(res => {
        console.log("registry", res)
    })
})

$("#token-factory").click(() => {
    const token = LwtUtils.builder.setClaims({
        userId: 1
    }).setExpiration(new Date().valueOf() + 1000 * 60 * 24 * 100).compact()

    console.log("token", token)

    localStorage.setItem("token", token)
})


$("#create-default-db").click(() => {
    const def_user = new User()

    def_user.id = 1
    def_user.imgUrl = "/statics/upload/user/default-user.png"
    def_user.learnTime = 12
    def_user.likeCount = 24
    def_user.name = "sleep"
    def_user.signature = "该用户只想抽个时间睡大觉"
    def_user.password = SHA256("123123")
    def_user.username = "moluer"
    def_user.reportCount = 999
    def_user.email = "moluer@github.com"

    mapper.save("tb_user", [
        def_user
    ])

    console.log("set default db, user", def_user)
})

$("#deal-data").click(() => {
    $.get("/data.json").then(res => {
        let idx = 1
        res.forEach(ele => {
            ele.id = (idx ++)
            ele.joinLearning = Math.floor(Math.max((Math.random() * 500000) % (Math.random() * 50000), 200) + (Math.random() * 1000))
        })

        console.log(res)
        BtMessage({
            type: 'success',
            message: 'deal data success'
        })  
    })
})
$("#create-data").click(() => {
    $.get("/data.json").then(res => {

        mapper.save("tb_course", res)
        BtMessage({
            type: 'success',
            message: 'deal data success'
        })  
    })
})

$("#api-course-test").click(() => {
    request({
        url: '/course/list/hot?tag=ict'
    }).then(res => {
        console.log(res)
    })
})


$("#test-createData").click(() => {

    BtMessage({
        type: 'info',
        message: '正在load..'
    })

    $.get("/data.json").then(res => {
        localStorage.setItem("$mydb_tb_course", JSON.stringify(res))
        BtMessage({
            type: 'success',
            message: 'table "tb_course" has loaded'
        })
    })
    $.get("/data_user.json").then(res => {
        localStorage.setItem("$mydb_tb_user", JSON.stringify(res))
        BtMessage({
            type: 'success',
            message: 'table "tb_user" has loaded'
        })
    })

})